package org.jeecg.modules.bobo.common.util;

/**
 * @program: jeecg-boot-parent
 * @description: 经纬度距离计算
 * @author: 三千霜
 * @create: 2020-08-03 15:09
 */
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
public class CaculateDistanceUtil {

    /**
     * 计算两点距离 返回单位为米
     * @author 三千霜
     * @date 2020/8/3 15:14
     * @param: gpsFrom
     * @param: gpsTo
     * @param: ellipsoid
     * @Return double
     * @throws

     */
    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
//    Sphere坐标系

    public static double getDistanceMeterBySphere(double gpsFromlongitude,double gpsFromlatitude, double gpsTolongitude,double gpsTolatitude){
        GlobalCoordinates gpsFrom = new GlobalCoordinates(gpsFromlatitude, gpsFromlongitude);
        GlobalCoordinates gpsTo = new GlobalCoordinates(gpsTolatitude, gpsTolongitude);
       return getDistanceMeter(gpsFrom,gpsTo,Ellipsoid.Sphere);
    }
//    WGS84
    public static double getDistanceMeterByWGS84(double gpsFromlongitude,double gpsFromlatitude, double gpsTolongitude,double gpsTolatitude){
        GlobalCoordinates gpsFrom = new GlobalCoordinates(gpsFromlatitude, gpsFromlongitude);
        GlobalCoordinates gpsTo = new GlobalCoordinates(gpsTolatitude, gpsTolongitude);
       return  getDistanceMeter(gpsFrom,gpsTo,Ellipsoid.WGS84);
    }
    //gcj02
    public static double getDistanceMeterByGcj02(double gpsFromlongitude,double gpsFromlatitude, double gpsTolongitude,double gpsTolatitude){
        //转换gcj02
        double[] f = GPSUtils.gcj02_To_Gps84(gpsFromlatitude, gpsFromlongitude);
        double[] t = GPSUtils.gcj02_To_Gps84(gpsTolatitude, gpsTolongitude);

        GlobalCoordinates gpsFrom = new GlobalCoordinates(f[0], f[1]);
        GlobalCoordinates gpsTo = new GlobalCoordinates(t[0], t[1]);
       return  getDistanceMeter(gpsFrom,gpsTo,Ellipsoid.WGS84);
    }
}
